#!/bin/bash
set -e 

vue3_img="vue3-img"
container_name="vue3_dev"
container_port=8080
volumn_dir=""
volumn_dir="E:\webroot\Vue3App"
volumn_dir=$(echo $volumn_dir | sed 's/\\/\\\\/g')

function build_img(){
    docker build -t $vue3_img .
}

function rm_img(){
    docker rmi $vue3_img
}

function start_app(){
    
    docker run -itd --name $container_name -p $container_port:8080 -v $volumn_dir:/app $vue3_img
}

function rm_app(){
 docker rm -f $container_name
}

# sleep(5)
function run_app(){
    command="cd /app && npm run dev:docker"
    docker exec -it $container_name sh -c "$command"
}

function help() {
    local old_IFS=$IFS
    IFS=' '
cat <<EOF
    Usage: manage.sh [options]
EOF
    echo  "\r"
    for word in $ALL_COMMAND; do
        echo  "\t$word\n"
    done
    echo  "\r"
cat <<EOF
    help   show this message
EOF
    #恢复IFS值
    IFS=$old_IFS
}

function list_contains() {
    local var="$1"
    local str="$2"
    local val
    eval "val=\" \${$var} \""
    [ "${val%% $str *}" != "$val" ]
}

action=${1:-help}

ALL_COMMAND="build_img rm_img start_app rm_app run_app"

list_contains ALL_COMMAND "$action" || action=help

$action "$@"